// pages/personal/index.js
const app = getApp()
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    login:false,
    userphoto:'',
    username:'',
    vipname:"",
    percent:0,
    nextvip:"距离下一会员等级",
    VipRule:[
      {name:"普通会员",price:"100.00",num:3},
      {name:"高级会员",price:"200.00",num:5},
      {name:"至尊会员",price:"500.00",num:10},
  ],
    viprule:true,
    nowequity:"暂无，快去提升会员等级吧！",
    nextequity:"自助饮料畅饮*1+随机小吃*1",
    equityhidden:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.render()
  },
  bindGetUserInfo (e) {
    let userInfo = e.detail.userInfo
    if(!app.globalData.login && userInfo){
      db.collection('users').add({
        data:{
          userphoto:userInfo.avatarUrl,
          username:userInfo.nickName,
          coupons:[],
          orders:[],
          monetary:0,
          favorite:[],
          vipname:'',
          num:0,
          shoppingCar:[],
        }
      }).then(res=>{
        db.collection('users').doc(res._id).get().then(res=>{
          app.userinfo = Object.assign(app.userinfo,res.data)
          app.globalData.login = true
          wx.showToast({
            title: '登录成功',
          })
          this.setData({
            userphoto:res.data.userphoto,
            username:res.data.username,
            login:true, 
          })
        })
        
      })
    }
  },
  vipruleChang(){
    this.setData({viprule:!this.data.viprule})
  },
  EquityChang(){
    this.setData({equityhidden:!this.data.equityhidden})
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.render()
  },
  render(){
    
    if(JSON.stringify(app.userinfo) != "{}"){
      let price = app.userinfo.monetary
      let num = app.userinfo.num
      let percent = price%100
      let nextvip = "距离下一会员等级"
      let vipname =""
      let nowequity = "暂无，快去提升会员等级吧！"
      let nextequity = "自助饮料畅饮*1+随机小吃*1"
      console.log(num);
       if(price>=500 || num>=10){
         nextvip = "您已是至尊用户"
         vipname = "至尊会员"
         percent = 100
         nowequity = "自助饮料畅饮*4+随机小吃*2"
         nextequity ="已到达最大权益"
      }else if(price>=200 || num>=5){
        if(num>=5&&price>=200){
          percent =  (price-200)/300*100
         percent = percent>(num-5)/5*100?percent :(num-5)/5*100
        }else{
          percent = 0
        }
        vipname = "高级会员"
        nowequity = "自助饮料畅饮*2+随机小吃*1"
        nextequity ="自助饮料畅饮*4+随机小吃*2"
     }else if(price>=100 || num>=3){
      if(num>=3&&price>=100){
        percent = percent>(num-3)/2*100?percent :(num-3)/2*100
      }else{
        percent = 0
      }
      vipname = "普通会员"
      nowequity = "自助饮料畅饮*1+随机小吃*1"
      nextequity ="自助饮料畅饮*2+随机小吃*1"
    }else if(num<3){
        percent = percent>num/3*100?percent:num/3*100
    }
      this.setData({
        username:app.userinfo.username,
        userphoto:app.userinfo.userphoto,
        login:true,
        percent:percent,
        nextvip:nextvip,
        vipname:vipname,
        nowequity:nowequity,
        nextequity:nextequity
      }) 
    }else{
    this.setData({
      login:app.globalData.login
    })
    }
    
  },
})